Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
'--- Created Date On : 12-Oct-2010 (Gyan)
'--- Modifyed Date   : 12-Oct-2010(Priyabrat)

Public Class frmPracticeStatisticCharts

    Private mintPatientId As Integer

    Public Sub New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        
    End Sub

    Private Sub frmPatientStatement_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub frmPatientStatement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            dteFromDate.EditValue = Today
            dteToDate.EditValue = Today

            Call PopulateCheckedListboxDX(chklistDrugs, "Drug Name", "DrugId", "LtblDrugs", "DrugId,DrugName[Drug Name]", "", "DrugName")

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnViewReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnViewReport.Click
        Try
            If IsValidEntry() = False Then Exit Sub

            Dim i As Integer
            Dim dt As DataTable = chklistDrugs.DataSource
            Dim IsFirstParameter As Boolean = True
            Dim strQuery As String = "SELECT LtblDrugs.DrugName as Medication, COUNT(tblVisitPrescription.DrugId)   AS NumberOfUsers  ,CAST(MIN(VisitDate)AS VARCHAR) + ' to ' + CAST(MAX(VisitDate) AS VARCHAR) as DateRange FROM tblVisitPrescription INNER JOIN LtblDrugs ON tblVisitPrescription.DrugId = LtblDrugs.DrugId INNER JOIN tblVisits ON tblVisitPrescription.VisitId = tblVisits.VisitId WHERE "

            'add where clause
            For i = 0 To dt.Rows.Count - 1
                If chklistDrugs.GetItemChecked(i) = True Then
                    If IsFirstParameter Then
                        strQuery = strQuery & " LtblDrugs.DrugId=" & dt.Rows(i).Item("DrugId")
                        IsFirstParameter = False
                    Else
                        strQuery = strQuery & " OR LtblDrugs.DrugId=" & dt.Rows(i).Item("DrugId")
                    End If
                End If
            Next
            If chkViewAll.Checked = True Then
                strQuery = strQuery & " Group BY DrugName"
            Else
                strQuery = strQuery & " AND " & " tblVisits.VisitDate  BETWEEN '" & dteFromDate.EditValue.ToShortDateString & " 00:00' AND '" & dteToDate.EditValue.ToShortDateString & " 23:59' Group BY DrugName"
            End If

            Call ShowStatisticsReport(strQuery, Me.ParentForm)

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Function IsValidEntry() As Boolean
        'Here check valid entry of fields
        Dim strMsg As String = ""
        IsValidEntry = False
        Dim NoUserChecked As Boolean = False
        With chklistDrugs
            For intCtr As Int16 = 0 To .ItemCount
                If .GetItemChecked(intCtr) Then
                    NoUserChecked = True
                    Exit For
                End If
            Next intCtr
        End With
        If NoUserChecked = True Then
            If chkViewAll.Checked = False AndAlso IsDBNullOrNothing(dteFromDate.EditValue) Then
                strMsg = "Please select From Date"
                dteFromDate.Focus()
            ElseIf chkViewAll.Checked = False AndAlso IsDBNullOrNothing(dteToDate.EditValue) Then
                strMsg = "Please select To Date"
                dteToDate.Focus()
            End If
        Else
            strMsg = "Please select option(s) to view report"
            chklistDrugs.Focus()
        End If

        If Trim(strMsg) <> "" Then
            IsValidEntry = False
            MessageBox.Show(strMsg, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            IsValidEntry = True
        End If
    End Function

    Private Sub chkViewAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkViewAll.CheckedChanged
        If chkViewAll.Checked Then
            dteFromDate.Enabled = False
            dteToDate.Enabled = False
        Else
            dteFromDate.Enabled = True
            dteToDate.Enabled = True
        End If
    End Sub

    Private Sub chkSelect_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelect.CheckedChanged
        If chkSelect.Checked = True Then

            chklistDrugs.CheckAll()
        ElseIf chkSelect.Checked = False Then
            chklistDrugs.UnCheckAll()
        End If
    End Sub
End Class
